//
//  BaseTableViewController.m
//  Root
//
//  Created by conkey on 2019/1/9.
//  Copyright © 2019 Root. All rights reserved.
//

#import "BaseTableViewController.h"

// const variable
NSString *const kQISBaseDefaultCellIdentifier = @"QISBaseDefaultCell";

@interface BaseTableViewController ()

// table view contraints
@property (strong, nonatomic) NSLayoutConstraint *topContraint;
@property (strong, nonatomic) NSLayoutConstraint *bottomContraint;

@end

@implementation BaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // for save items
    if (_items == nil) {
        self.items = [[NSMutableArray alloc] init];
    }
    
    if (_isGroupedStyle) {
        [self setupTableViewForStyle:UITableViewStyleGrouped];
    }
    else {
        [self setupTableViewForStyle:UITableViewStylePlain];
    }
    
}

// helper
- (void)setupTableViewForStyle:(UITableViewStyle)style
{
    // table view
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:style];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.translatesAutoresizingMaskIntoConstraints = NO; // use auto layout
    // hide separator line if empty cell.
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; // show separator line in last cell
    [self.view addSubview:self.tableView];
    
    self.tableView.separatorColor = YYKTableViewSeparatorColor;
    
    // iOS8 self sizing cells
    self.tableView.estimatedRowHeight = 44.0; // default row height
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // for iOS 11
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    
    // register default cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kQISBaseDefaultCellIdentifier];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    // auto layout table view
    [self.view pinSubview:self.tableView toEdge:NSLayoutAttributeLeading];
    [self.view pinSubview:self.tableView toEdge:NSLayoutAttributeTrailing];
    // saving constraint for update it
    self.topContraint = [self.view pinSubview:self.tableView toEdge:NSLayoutAttributeTop];
    self.bottomContraint = [self.view pinSubview:self.tableView toEdge:NSLayoutAttributeBottom];
    
    // update
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
}

- (void)adjustTableViewWithTop:(CGFloat)top bottom:(CGFloat)bottom
{
    CGFloat topOffset = -top;
    CGFloat bottomOffset = bottom;
    self.topContraint.constant = topOffset;
    self.bottomContraint.constant = bottomOffset;
    
    // update
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraintsIfNeeded];
}

#pragma mark - table view source delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
}

#pragma mark - table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // deselect animate
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
